define(['ldfw/core','art','text!tpl/notification/detail.tpl','comps/ajax','comps/session'],function(LDFW,art,detailTpl,ajax,session){
    var DetailView = LDFW.Container.extend({
        className:'panel noticeDetail-panel',
        events:{
            'click [type="submit"]':'confirm',
            'click [type="confirmBuy"]':'confirmBuy',
            'click [type="cancelBuy"]':'cancelBuy',
            'click [type="submitVisit"]':'confirmVisit'
        },
        initialize:function(){
            const _this = this
            this.on('shown',this.loadData)
            this.model.set('company_name',current_company)
        },
        rendered:function(){
        },
        loadData:function(){
            const _this = this
            ajax.post('cpliL.3.101',{notice_no:this.notice_no}).success(function(resp){
                if(resp.result){
                    let noticeStr = resp.result.notice_variable.replace(/\!/g,'[')
                        .replace(/</g,'{')
                        .replace(/>/g,'}')
                        .replace(/\//g,']'),
                        notice = JSON.parse('{' + noticeStr + '}');
                    notice.confirm_status = resp.result.confirm_status
                    notice.notice_type = resp.result.notice_type
                    notice.cust_type = resp.result.cust_type
                    notice.cust_name = resp.result.cust_name
                    _this.model.set(notice)
                    //查询该客户被认证为投资者的时间
                    if(notice.notice_type == 2 || notice.notice_type == 3){
                        ajax.post('cpliL.3.103').success(function (resp2) {
                            if (!_.isEmpty(resp2.result)) {
                                var audit = resp2.result[0]
                                _this.model.set('identified_date',audit.audit_date)
                                let html = art.compile(detailTpl)(_this.model.toJSON())
                                _this.$el.html($(html))
                            }
                        })
                    //电子回访当
                    }else if(notice.notice_type===7){
                        ajax.post('getclientCoolElectronVisit.do',{
                            relation_field:_this.notice_no
                        }).success(function(resp2){
                            _this.model.set('question_result',resp2.question_result)
                            _this.model.set('visit_back_ret',resp2.visit_back_ret)
                            let html = art.compile(detailTpl)(_this.model.toJSON())
                            _this.$el.html($(html))
                        })
                    //适当性回访
                    }else if(notice.notice_type===8){
                        ajax.post('getclientSdxElectronVisit.do',{
                            relation_field:_this.notice_no
                        }).success(function(resp2){
                            _this.model.set('question_result',resp2.question_result)
                            _this.model.set('visit_back_ret',resp2.visit_back_ret)
                            let html = art.compile(detailTpl)(_this.model.toJSON())
                            _this.$el.html($(html))
                        })
                    }else{
                        let html = art.compile(detailTpl)(_this.model.toJSON())
                        _this.$el.html($(html))
                    }
                }
            })
        },
        setNoticeNo:function(notice_no){
            this.notice_no = notice_no
        },
        confirm:function(){
            if (this.model.get('confirm_status') == 2) {
                ajax.post('cpliL.3.11', {notice_no: this.notice_no})
                    .success(function(resp){
                    session.flush()
                    LDFW.navigate('notification')
                })
            }else{
                LDFW.navigate('notification')
            }
        },
        cancelBuy:function(){
            const _this = this
            let data={
                qst_ans_str:'',
                paper_no:this.model.get('paper_no'),
                notice_no: this.notice_no,
                electron_visit_no:this.model.get('electron_visit_no'),
                cust_no:this.model.get('cust_no'),
                visit_back_ret: 6
            }
            $('.confirmation_form input:checked').each((index,el)=>{
                data.qst_ans_str = data.qst_ans_str + $(el).attr('name')+','+$(el).val()+';'
            })
            ajax.post('cpliL.1.44', data).success(function(resp){
                if(!resp.resultCode) {
                    _this.confirm()
                }
            })
        },
        confirmVisit:function(){
            const _this = this
            let data={
                qst_ans_str:'',
                paper_no:this.model.get('paper_no'),
                notice_no: this.notice_no,
                electron_visit_no:this.model.get('electron_visit_no'),
                cust_no:this.model.get('cust_no'),
                visit_back_ret: 8
            }
            $('.confirmation_form input:checked').each((index,el)=>{
                data.qst_ans_str = data.qst_ans_str + $(el).attr('name')+','+$(el).val()+';'
            })
            ajax.post('cpliL.1.66', data).success(function(resp){
                if(!resp.resultCode) {
                    _this.confirm()
                }
            })
        },
        confirmBuy:function(){
            const _this = this
            let data={
                qst_ans_str:'',
                paper_no:this.model.get('paper_no'),
                notice_no: this.notice_no,
                electron_visit_no:this.model.get('electron_visit_no'),
                cust_no:this.model.get('cust_no'),
                visit_back_ret: 5
            },
            allAccept = true
            $('.confirmation_form input:checked').each((index,el)=>{
                data.qst_ans_str = data.qst_ans_str + $(el).attr('name')+','+$(el).val()+';'
                if($(el)[0].checked && $(el).val()!='A'){
                    allAccept = false
                    return
                }
            })
            if(!allAccept){
                $.alert('您有未确认的风险，请确认后再购买')
            }else{
                ajax.post('cpliL.1.44', data).success(function(resp){
                    if(!resp.resultCode) {
                        _this.confirm()
                    }
                })
            }
        }
    })
    return new DetailView()
})